<?php
//require_once 'Zend/View/Helper/Abstract.php';

class Zend_View_Helper_Grid extends Zend_View_Helper_Abstract
{
    public $view = null;

    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
        return $this;
    }

    /* this constructor takes 4 args,
     * name of the grid, header columns,
     * paginator and the bool value sorting. */
    public function grid ($name, $fields = null, $paginator = null, $sorting = false, $options = false)
    {
        // taking value of sort using Fron controller getRequest() method.
        $sort = Zend_Controller_Front::getInstance()
                ->getRequest()
                ->getParam('sort','DESC');

        // checking and handling sorting.
        if ($sort  ==  'ASC') {
            $sort = 'DESC';
        }  else  {
            $sort = "ASC";
        }

        // start constructing the gird.
        //$output = "<div id='".$name."'>";
        $output = "";
        $output .= "<table class='grid'>
                    <thead>
                        <tr>";

        // this foreach loop display the column header  in "th" tag.
        foreach ($fields as $key => $value)  {
            $output .= "<th>";
            // check if sorting is true, if so add link to the fields headers
            if ($sorting){
            $output .= "<a href='".$this->view->url(
                array(
                    'sort' => $sort,
                    'by' => $key
                )
            )."'>".$value."</a>";
            
            } else {
                $output .= $value;
            }
            $output .= "</th>";
        }

        if (is_array($options)) {
           $output .= "<th>";
           $output .= $options['label'];
           $output .= "</th>";
        }

        $output .= "</tr>
                    </thead>";

        // constructing the body that contain the records in rows and columns.

        $output .="<tbody>";

        // this loop display the actual data.

        foreach ($paginator as $p) {
            $output.="<tr>";
            foreach ($p as $key => $record) {
                if(isset($fields[$key])){
                    $output .= "<td>".$record."</td>";
                }
            }

            if (is_array($options)) {
                $output .= '<td>';
                if (is_array($options['options'])) {
                    foreach ($options['options'] AS $label => $url) {
                        $output .= '<div style="float:right;margin-left:10px;">';
                        $url = str_replace("%7BID%7D", $p[$options['primary']], $url);
                        $output .= '<a href="'.$url.'">'.$label.'</a>';
                        $output .= '</div>';
                    }
                }
                $output .= "</td>";
            }

            $output.="</tr>";
        }

        $output .= "</tbody>";

        // check if paginator is ture, if so then add
        //paginator component to the table "tfoot".

        if($paginator) {
            $optionField = is_array($options) ? 1 : 0;
            $output .="<tfoot>";
            $output .="<td align='center' colspan='".(count($fields) + $optionField)."'>";
            $output.= $this->view->paginationControl(
                $paginator,
                'Sliding',
                'pagination.phtml'
            );
            $output .="</td>";
            $output .="</tfoot>";
        }

        $output .= "</table>";
        //$output .= "<div>";
        return $output;
    }
    
}